package com.awise.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.awise.R;
import com.awise.util.Constant;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.util.Timer;
import java.util.TimerTask;


/**
 * @author lgz
 * @time 2018/1/22 21:06
 */
public abstract class BaseActivity extends Activity {
    protected KProgressHUD mLoadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 23) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.color_white));
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    /**
     * 查找组件
     *
     * @param viewId：View的ID
     * @return View
     */
    @SuppressWarnings("unchecked")
    protected <view extends View> view getViewById(int viewId) {
        return (view) findViewById(viewId);
    }

    /**
     * 启动Activity
     *
     * @param context：Context
     * @param cls：Class<?>
     * @param isFinish：是否关闭Activity
     */
    protected void startActivityByIntent(Context context, Class<?> cls, Boolean isFinish) {
        startActivity(new Intent(context, cls));
        if (isFinish) {
            finish();
        }
    }

    /**
     * 显示短Toast
     *
     * @param context：Context
     * @param text：String
     */
    protected void showShortToastByString(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示长Toast
     *
     * @param context：Context
     * @param text：String
     */
    protected void showLongToastByString(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }


 /*   *//**
     * 初始化View
     *//*
    public abstract void initView();

    *//**
     * 初始化数据
     *//*
    public abstract void initData();
*/
    /**
     * 设置SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param value：值
     */
    protected void setSP(Context context, String key, int value) {
        SharedPreferences.Editor et = context.getSharedPreferences(Constant.APPNAME, Context.MODE_PRIVATE).edit();
        et.putInt(key, value);
        et.apply();
    }

    /**
     * 设置SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param value：值
     */
    protected void setSP(Context context, String key, String value) {
        SharedPreferences.Editor et = context.getSharedPreferences(Constant.APPNAME, Context.MODE_PRIVATE).edit();
        et.putString(key, value);
        et.apply();
    }

    /**
     * 设置SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param value：值
     */
    protected void setSP(Context context, String key, Boolean value) {
        SharedPreferences.Editor et = context.getSharedPreferences(Constant.APPNAME, Context.MODE_PRIVATE).edit();
        et.putBoolean(key, value);
        et.apply();
    }

    /**
     * 获取SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param defaultValue：值
     * @return 值
     */
    protected int getSP(Context context, String key, int defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defaultValue);
    }


    /**
     * 获取SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param defaultValue：值
     * @return 值
     */
    protected String getSP(Context context, String key, String defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME, Context.MODE_PRIVATE);
        return sp.getString(key, defaultValue);
    }

    /**
     * 获取SharedPreferences
     *
     * @param context：Context
     * @param key：键
     * @param defaultValue：值
     * @return 值
     */
    protected boolean getSP(Context context, String key, Boolean defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defaultValue);
    }

    /**
     * 显示加载框
     */
    public void showLoadingView() {
        if (mLoadingDialog == null) {
            mLoadingDialog = KProgressHUD.create(this)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setDimAmount(0.5f)
                    .setMaxProgress(100);
        }
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }

    }

    /**
     * 消失加载框
     */
    public void dismissLoadingView() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }


    Timer timer;
    public void closedLoadingView() {
        timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run () {
                dismissLoadingView();
                stopTimer();
            }
        },500,500);
    }
    // 停止定时器
    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            // 一定设置为null，否则定时器不会被回收
            timer = null;
        }
    }
}